// packageStore/pages/distribution/rank/index.js
import BasePage from '../../../js/common/base-page';
import { distribution} from "../../../js/modules/distribution.js";
BasePage({

  /**
   * 页面的初始数据
   */
  data: {
    tablist: ['邀请榜', '佣金榜'],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 10,
    slidewidth: 0,
    tabsublist: ['总排名', '本周','本月','上月'],
    activeSubIndex: 0,
    sliderSubOffset: 0,
    sliderSubLeft: 10,
    slideSubWidth: 0,
    totalNum:500,
    totalRank:25,
    type: 1,
    status: 0,
    lists: [{
      status: 0, //总排名
      list: [],
      hasMoreData: true,
      page: {
        PageIndex: 1,
        PageSize: 20,
        keywords: "",
        sortField: "",
        sortDirect: "desc",
        TimeType:0
      }
    },
    {
      status: 1, //本周
      list: [],
      hasMoreData: true,
      page: {
        PageIndex: 1,
        PageSize: 20,
        keywords: "",
        sortField: "",
        sortDirect: "desc",
        TimeType:1
      }
    },
     {
      status: 2, //本月
      list: [],
      hasMoreData: true,
      page: {
        PageIndex: 1,
        PageSize: 20,
        keywords: "",
        sortField: "",
        sortDirect: "desc",
        TimeType:2
      }
    },
    {
      status: 3, //上月
      list: [],
      hasMoreData: true,
      page: {
        PageIndex: 1,
        PageSize: 20,
        keywords: "",
        sortField: "",
        sortDirect: "desc",
        TimeType:3
      }
    }
  ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    wx.getSystemInfo({
      success: (res)=> {
        that.setData({
          slideSubWidth: (res.windowWidth / that.data.tabsublist.length) - 20,
        });
      }
    });
    this.loadList();
  },
  loadList() {
    var that = this;
    var current = this.data.lists[that.data.activeSubIndex];
    var param = current.page;
    distribution.loadDistributeRank(this.data.type,param, function(content) {
      var hasMoreData = content.count > (current.page.PageIndex) * current.page.PageSize;
      var list_key = "lists[" + that.data.activeSubIndex + "].list";
      //是否有更多数据
      var list_has = "lists[" + that.data.activeSubIndex + "].hasMoreData";
      //无数据时显示的信息
      var isLoadComplete = "lists[" + that.data.activeSubIndex + "].isLoadComplete";
      var index = that.data.lists[that.data.activeSubIndex].list.length;
      var data = content.data;
      //当分页大于1时，累加
      if (current.page.PageIndex > 1) {
        data = that.data.lists[that.data.activeSubIndex].list.concat(data);
      }
      //如果key为变量作为key值的时候要用[]包裹起来
      that.setData({
        [list_key]: data,
        [list_has]: hasMoreData,
        [isLoadComplete]: true
      });

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    this.data.lists[this.data.activeSubIndex].page.PageIndex = 1;
    this.loadList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function(e) {
    var current = this.data.lists[this.data.activeSubIndex];
    if (current.hasMoreData) {
      var index = this.data.lists[this.data.activeSubIndex].page.PageIndex;
      this.data.lists[this.data.activeSubIndex].page.PageIndex = index + 1;
      this.loadList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 切换当前选择项(一级)
   */
  changeCurrent: function (res) {
    var that=this
    if (this.data.activeIndex == res.detail.currentNum.id) return;
    var activeIndex = res.detail.currentNum.id;
    //重置列表
    var lists = that.data.lists;
    for(let i=0;i<lists.length;i++){
      lists[i].list = [];
      lists[i].hasMoreData = true;
      lists[i].page.PageIndex = 1;
    };
    this.setData({
      sliderOffset: res.detail.currentNum.offsetLeft,
      activeIndex: activeIndex,
      type:activeIndex==0?1:0,
      lists:lists
    },()=>{
      that.loadList()
    });
  },
  /**
   * 切换当前选择项(二级)
   */
  changeSubCurrent: function (res) {
    var that=this
    if (this.data.activeSubIndex == res.detail.currentNum.id) return;
    var activeIndex = res.detail.currentNum.id;
    this.setData({
      sliderSubOffset: res.detail.currentNum.offsetLeft,
      activeSubIndex: activeIndex
    },()=>{
      var current = that.data.lists[activeIndex];
      if (current.hasMoreData) {
        that.loadList()
      }
    });
  }
})